package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.AdminOrderService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/order")
@CrossOrigin
@RestController
public class AdminOrderController {

    @Autowired
    private AdminOrderService adminOrderService;

    /**
     * 订单列表
     * @param page
     * @param limit
     * @param userId
     * @param orderSn
     * @param orderStatusArray
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:order:list")
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "订单列表")
    public Object orderList(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            Integer userId,String orderSn,Integer[] orderStatusArray,
                            String sort,String order){
        IPage<DtsOrder> iPage = adminOrderService.selectOrderList(page,limit,userId,orderSn,orderStatusArray,sort,order);

        long total = iPage.getTotal();
        List<DtsOrder> records = iPage.getRecords();

        DtsOrder[] items = new DtsOrder[records.size()];

        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }


    //快递列表
    @GetMapping("/listShipChannel")
    @RequiresPermissions("admin:order:listShipChannel")
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "快递列表")
    public Object orderListShipChannel(){
        HashMap<String, Object> data = new HashMap<>();

        Object[] shipChannelList = new Object[4];

        String[] value = {"HTKY","ZTO","YTO","YD",};
        String[] label = {"百世快递","中通快递","圆通速递","韵达速递",};

        for (int i = 0; i < shipChannelList.length; i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("value", value[i]);
            map.put("label", label[i]);
            shipChannelList[i] = map;
        }

        data.put("shipChannelList", shipChannelList);
        return ResponseUtil.ok(data);
    }

    //查询订单详情
    @GetMapping("/detail")
    @RequiresPermissions("admin:order:detail")
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "订单详情")
    public Object orderDetail(Integer id){

        List<DtsOrderGoods> orderGoods = adminOrderService.selectOrderGoodes(id);

        DtsOrder order = adminOrderService.selectOrder(id);

        DtsUser user = adminOrderService.selectUser(order.getUserId());

        HashMap<String, Object> data = new HashMap<>();

        data.put("orderGoods", orderGoods);
        data.put("user", user);
        data.put("order", order);

        return ResponseUtil.ok(data);
    }

    //发货
    @PostMapping("/ship")
    @RequiresPermissions("admin:order:ship")
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "订单发货")
    public Object orderShip(@RequestBody DtsOrder dtsOrder){
        adminOrderService.updateOrder(dtsOrder);
        return ResponseUtil.ok();
    }

}
